package com.caiwm.client.view;

import com.caiwm.client.common.MessageEntity;
import com.caiwm.client.common.MessageType;
import com.caiwm.client.dialog.CaiwmDialog;
import com.caiwm.client.protocol.TCPUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * <pre>
 * ━━━━━━神兽出没━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃  神兽保佑
 * 　　　　┃　　　┃  代码无bug
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━感觉萌萌哒━━━━━━
 * </pre>
 *
 * @author shiqla
 * @Desc 私聊窗口
 * @Date 2019/1/14/014
 */
public class PrivateChatView  extends CaiwmDialog implements ActionListener {

    private JFrame jFrame;

    private String toUserName;

    private String userName;

    private ChatView chatView;

    private JTextArea showMessageTextArea;

    private JScrollPane jScrollPane;

    private JTextArea inputMessageTextArea;

    private JButton sendButton;

    public PrivateChatView(String toUserName,String userName,ChatView chatView){
        this.toUserName = toUserName;
        this.userName = userName;
        this.chatView = chatView;
        jFrame = new JFrame(this.toUserName);
        jFrame.setLayout(null);

        this.showMessageTextArea = new JTextArea();
        this.showMessageTextArea.setBounds(0, 0, 560, 200);
        this.showMessageTextArea.setLineWrap(true);
        this.showMessageTextArea.setBackground(new Color(38, 255, 161));
        this.showMessageTextArea.setEditable(false);
        this.showMessageTextArea.setBorder(BorderFactory.createLineBorder(Color.gray, 5));

        jScrollPane = new JScrollPane(this.showMessageTextArea);
        jScrollPane.setBounds(0, 0, 560, 200);

        this.inputMessageTextArea = new JTextArea();
        this.inputMessageTextArea = new JTextArea();
        this.inputMessageTextArea.setBounds(0, 210, 560, 100);
        this.inputMessageTextArea.setLineWrap(true);
        this.inputMessageTextArea.setBackground(new Color(255, 255, 255));
        this.inputMessageTextArea.setBorder(BorderFactory.createLineBorder(Color.gray, 5));



        this.sendButton = new JButton("发送");
        this.sendButton.setBounds(450,320,100,30);

        this.sendButton.addActionListener(this);

        this.jFrame.add(this.jScrollPane);
        this.jFrame.add(this.inputMessageTextArea);
        this.jFrame.add(this.sendButton);
    }

    public void start(String message){
        this.showMessageTextArea.setText(message);

        //给 私聊窗口添加一个关闭时间,不要退出整个程序
        this.jFrame.addWindowListener(new WindowAdapter() {

            private PrivateChatView privateChatView;

            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                this.privateChatView.close();
            }

            WindowAdapter init(PrivateChatView privateChatView){
                this.privateChatView = privateChatView;
                return this;
            }
        }.init(this));

        jFrame.setSize(580, 400); //设置窗口的大小
        jFrame.setLocation(300,200);//设置窗口的初始位置
//        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口

        System.out.println(this.toUserName + "   " + this.userName);
        jFrame.setVisible(true);

    }

    public void close(){
        for (PrivateChatView privateChatView : this.chatView.getPrivateChatViewList()) {
            if(privateChatView.getToUserName().equals(this.toUserName)){
                this.chatView.getPrivateChatViewList().remove(privateChatView);
                break;
            }
        }
        this.jFrame.dispose();
    }


    public void actionPerformed(ActionEvent e) {

        String message = this.showMessageTextArea.getText();

        StringBuilder stringBuilder = new StringBuilder(message);
        String sendMessage = this.inputMessageTextArea.getText();
        if(sendMessage == null || sendMessage.equals("")){
            this.errorMessageDialog("发送的消息不能为空!");
        }else{
            stringBuilder.append("你的消息:\n");
            stringBuilder.append("      "+ sendMessage + "\n");
            this.showMessageTextArea.setText(stringBuilder.toString());
            this.inputMessageTextArea.setText("");
            MessageEntity messageEntity = new MessageEntity(this.userName,this.toUserName,MessageType.privateMessage,sendMessage);
            TCPUtils.getInstance().sendMessage(messageEntity);
        }

    }

    public String getToUserName() {
        return toUserName;
    }

    public void setToUserName(String toUserName) {
        this.toUserName = toUserName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public JTextArea getShowMessageTextArea() {
        return showMessageTextArea;
    }

    public void setShowMessageTextArea(JTextArea showMessageTextArea) {
        this.showMessageTextArea = showMessageTextArea;
    }
}
